home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 11 - 1995 / 11.07 Jul 95 / Apple Guide Extensions / Context Check (CW) / contextCheck.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-06  |  4.0 KB  |  147 lines  |  [TEXT/MMCC]

  1. /**********************************************************************
  2.  
  3.      Real World Apple Guide
  4.      by Jesse Feiler
  5.  
  6.      Copyright © 1995 Philmont Software Mill. All Rights Reserved.
  7.      
  8.      ISBN 1-55851-429-5
  9.  
  10.      To order additional copies, call M&T Books, 1-800-488-5233
  11.      
  12.      For updates, check the bulletin board in the Philmont Software Mill
  13.      area on eWorld (shortcut "Philmont" or path "Computer Center : Straight to the 
  14.      Source : Philmont Software Mill").
  15.      
  16.      Send bug reports to ePhilmont@eWorld.com, or post to the bulletin
  17.      board in the Philmont Software Mill area on eWorld.
  18.  
  19. **********************************************************************/
  20.  
  21. #include    "contextCheck.h"
  22.  
  23. enum {
  24.      compareSignatureOnly = 0,
  25.      compareMajorRevOnly = 1,
  26.      compareMajorAndMinorRev = 2,
  27.      compareAllRevs = 3
  28. };
  29.  
  30. /*    prototypes */
  31. Boolean VersionAppRunning (short whichCompare, OSType signature, UInt8 majorRev, UInt8 minorRev, UInt8 bugRev);
  32. OSErr     SetContextResult(void* theData, Size theSize, Ptr* outMessage, Size* outSize);
  33.  
  34. pascal OSErr
  35. main(contextCheckData* msg, Size inSize, void* outMessage,Size* outSize, Handle ignoreMe )
  36. {
  37.     OSErr        err     =    errAECorruptData;    //    the default return value of the operation
  38.     Boolean        result     =     FALSE;                //    the default return value of the context check
  39.     
  40.     result = VersionAppRunning (msg->whichCompare, msg->signature, msg->majorRev, msg->minorRev, msg->bugRev);
  41.  
  42.     err    = SetContextResult(&result, sizeof(Boolean), outMessage, outSize);
  43.     
  44.     return(err);
  45. }
  46.  
  47.  
  48. //    Package the result for Apple Guide
  49. OSErr SetContextResult(void* theData, Size theSize, Ptr* outMessage, Size* outSize)
  50. {
  51.     Ptr    p;
  52.     
  53.     if (p = NewPtr(theSize))
  54.     {
  55.         BlockMove(theData, p, theSize);
  56.         
  57.         *outSize    =    theSize;
  58.         *outMessage    =    p;
  59.         
  60.         return(noErr);
  61.     }
  62.     else
  63.         return(MemError()); //A reasonable assumption--if we cannot allocate the
  64.                             //pointer for the result, we're out of memory.
  65. }
  66.  
  67. Boolean VersionAppRunning (short whichCompare, OSType signature, UInt8 majorRev, UInt8 minorRev, UInt8 bugRev)
  68. {
  69.     Boolean                result = FALSE;
  70.     ProcessSerialNumber    PSN;
  71.     ProcessInfoRec        info;
  72.     OSErr                err;
  73.     Str31                aProcessName;
  74.     FSSpec                aProcessAppSpec;
  75.  
  76.     PSN.highLongOfPSN = 0;
  77.     PSN.lowLongOfPSN = kNoProcess;
  78.     info.processInfoLength = sizeof(ProcessInfoRec);
  79.     info.processName = aProcessName;
  80.     info.processAppSpec = &aProcessAppSpec;
  81.  
  82.     while (GetNextProcess(&PSN) == noErr)
  83.         {
  84.             if(GetProcessInformation(&PSN, &info) == noErr)
  85.             {
  86.                 if(info.processSignature == signature)
  87.                 {
  88.                     switch (whichCompare)
  89.                         {
  90.                         case compareSignatureOnly:
  91.                             result = TRUE;
  92.                             break;
  93.                         
  94.                         default:
  95.                             {
  96.                             short         appResource = FSpOpenResFile (&aProcessAppSpec, fsRdPerm);
  97.                             short         myResError = ResError();
  98.                             if (myResError == noErr)
  99.                                 {
  100.                                 NumVersion    theVersion;
  101.                                 VersRecHndl aVersRecHndl = (VersRecHndl)GetResource ('vers', 1);
  102.                                 if (aVersRecHndl)
  103.                                     {
  104.                                     MoveHHi ((Handle)aVersRecHndl);
  105.                                     HLock ((Handle)aVersRecHndl);
  106.                                     theVersion = ((**aVersRecHndl).numericVersion);
  107.                                     HUnlock ((Handle)aVersRecHndl);
  108.                                     };
  109.                                 CloseResFile (appResource);
  110.                                 
  111.                                 switch (whichCompare)
  112.                                     {
  113.                                     case compareMajorRevOnly:
  114.                                         {
  115.                                         result = (majorRev == theVersion.majorRev);
  116.                                         break;
  117.                                         };
  118.                                     
  119.                                     case compareMajorAndMinorRev:
  120.                                         {
  121.                                         result = (majorRev == theVersion.majorRev)
  122.                                             && (minorRev == theVersion.minorAndBugRev >> 4);
  123.                                         break;
  124.                                         };
  125.                                         
  126.                                     case compareAllRevs:
  127.                                         {
  128.                                         UInt8        versionComparer = 0;
  129.                                         versionComparer = (minorRev << 4) + bugRev;
  130.                                         result = (majorRev == theVersion.majorRev)
  131.                                             && (versionComparer == theVersion.minorAndBugRev);
  132.                                         break;
  133.                                         };
  134.                                     };
  135.                                 };
  136.                                 break; // out of default case
  137.                             };
  138.                         };//switch statement
  139.                     break; //to escape the while loop
  140.                     }; //if we found the app
  141.                 }; // if we got the process info
  142.             }; //while
  143.     
  144.     return result;
  145. }
  146.  
  147.